package com.sjy.MyBlog.web;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageInfo;
import com.sjy.MyBlog.bean.ArticleWithBLOBs;
import com.sjy.MyBlog.bean.Result;
import com.sjy.MyBlog.bean.Subscribe;
import com.sjy.MyBlog.biz.impl.ArticleBizImpl;
import com.sjy.MyBlog.biz.impl.SubscribeBizImpl;
import com.sjy.MyBlog.biz.impl.UserBizImpl;

@Controller
@SessionAttributes("currentPage")
public class IndexAction {

	@Resource
	private UserBizImpl ub;
	@Resource
	private ArticleBizImpl ab;
	@Resource
	private SubscribeBizImpl sb;
	
	@GetMapping(path= {"/","index","index.*"})
	public String toIndex(Model m,@SessionAttribute(name="currentPage",required=false) String currentPage) {
		m.addAttribute("user", ub.selectUser());
		m.addAttribute("defaultHead", "assets/images/profile.png");
		if(currentPage == null || currentPage == "") {
			m.addAttribute("currentPage", "indexPlugin::indexBody");
		}
		return "index";
	}
	
	@GetMapping("loadIndexPlugin.do")
	public String indexPlugin(Model m,
			@SessionAttribute(name="currentPage") String currentPage,
			@SessionAttribute(name="articleId",required=false) Integer id) {
		if(currentPage.equals("indexPlugin::indexBody")) {
			m.addAttribute("articlePages", ab.selectArticleByPage(1, 5));
		}else if(currentPage.equals("blogpost::articleBody")) {
			m.addAttribute("articleWithBlobs", ab.selectArticleById(id));
		}else if(currentPage.equals("blog-list::listBody")) {
			PageInfo<ArticleWithBLOBs> articlePages = new PageInfo<ArticleWithBLOBs>(ab.selectArticleByPage(1, 1));
			m.addAttribute("articlePages", articlePages);
		}else if(currentPage.equals("about::aboutBody")) {
			m.addAttribute("user", ub.selectUser());
		}
		
		
		return currentPage;
	}

	@GetMapping("toLodeIndex.do")
	public String toLodeIndex(Model m,@SessionAttribute("currentPage") String currentPage) {
		currentPage = "indexPlugin::indexBody";
		m.addAttribute("currentPage", currentPage);
		m.addAttribute("articlePages", ab.selectArticleByPage(1, 5));
		return currentPage;
	}
	
	@Autowired
	private JavaMailSender mailSender;
	@Value("${mail.formMail.addr}")
	private String from;
	@Value("${mail.formMail.subContent}")
	private String content;
	
	@Value("${mail.formMail.cancelSubscribeContent}")
	private String cancelSubscribeContent;
	
	@GetMapping("toSubscribe.do")
	public String toSubscribe(String email,Subscribe subscribe,Model m) {
		
		Result result = sb.addSubscribe(subscribe);
		m.addAttribute("result", result);
		if(result.getSucess()==0) {
			return "indexPlugin::alertBox";
		}
		String subject="博客订阅邮件提醒";
		System.out.println(email);
		SimpleMailMessage message = new SimpleMailMessage();
		System.out.println(1);
		message.setFrom(from);
		System.out.println(1);
		message.setTo(email);
		System.out.println(2);
		message.setSubject(subject);
		System.out.println(3);
		message.setText(content+"?email="+email);
		System.out.println(4);
		mailSender.send(message);
		System.out.println(5);
		return "indexPlugin::alertBox";
	}
	
	@GetMapping("cancelSubscribe.do")
	public String cancelSubscribe(Subscribe subscribe) {
		sb.cancleSubscribe(subscribe);
		
		String subject="博客取消订阅提醒";
		
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);
		message.setTo(subscribe.getEmail());
		message.setSubject(subject);
		message.setText(cancelSubscribeContent);
		mailSender.send(message);
		return "cancleSub";
	}
	
	
	@GetMapping("getTel.do")
	public String getTel() {
		return "indexPlugin::alertTelBox";
	}
	
}
